package com.lebang.service;

import com.alibaba.fastjson.JSON;
import com.lebang.common.util.AmountUtil;
import com.lebang.common.util.ShowDateUtil;
import com.lebang.component.MultiwordComponent;
import com.lebang.component.RedisClient;
import com.lebang.component.RedisSdkClient;
import com.lebang.entity.member.LbbMember;
import com.lebang.entity.member.LbbMemberRecord;
import com.lebang.entity.task.*;
import com.lebang.example.LbbCommonExample;
import com.lebang.example.LbbMemberRecordExample;
import com.lebang.example.LbbSosoExample;
import com.lebang.example.TaskCommentsExample;
import com.lebang.mapper.*;
import com.lebang.model.*;
import com.lebang.model.enums.Booleans;
import com.lebang.model.enums.HistoryTaskCategory;
import com.lebang.model.enums.buss.TaskCategory;
import com.lebang.model.enums.buss.TaskStatus;
import com.lebang.service.task.TaskService;
import com.lebangweb.common.DateUtil;
import com.lebangweb.common.TextUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

import static com.lebang.component.CommonComponent.TASK_COMMON_AMOUNT;
import static com.lebang.service.task.impl.CommentsServiceImpl.TASK_COMMENTS_AMOUNT;
import static com.lebang.service.task.impl.SosoServiceImpl.TASK_SOSO_AMOUNT;

/**
 * @author Eliohn on 2019/8/29.
 * @version 1.0
 */
@Service
public class WebApiService {
    @Autowired
    private LbbCommonMapper commonMapper;
    @Autowired
    private TaskCommentsMapper taskCommentsMapper;
    @Autowired
    private LbbSosoMapper sosoMapper;
    @Autowired
    private RedisClient redisClient;
    @Autowired
    private MultiwordComponent multiwordComponent;
    @Autowired
    private LbbMemberRecordMapper lbbMemberRecordMapper;
    @Autowired
    @Qualifier("commonTaskService")
    private TaskService<LbbCommon> commonTaskService;
    @Autowired
    @Qualifier("commentsTaskService")
    private TaskService<TaskComments> commentsTaskService;
    @Autowired
    @Qualifier("sosoTaskService")
    private TaskService<LbbSosoWithBLOBs> sosoTaskService;
    @Autowired
    private TaskInRedisService taskInRedisService;
    @Autowired
    private TaskScreenshotMapper taskScreenshotMapper;
    @Autowired
    private RedisSdkClient redisSdkClient;
    @Autowired
    private LbbMemberMapper lbbMemberMapper;

    public static final String WEB_TASK = "WEB_TASK";

    /**
     * 取任务列表
     *
     * @param userid
     * @param brand
     * @param userAllHistoryMoney
     * @return
     */
    public List<WebTask> getTaskList(String userid, String brand, int userAllHistoryMoney) {
        Set<String> list = redisClient.zrange(WEB_TASK, 0, -1);
        List<WebTask> taskList = new ArrayList<>(1);
        if (list == null || list.size() == 0) {
            taskInitToRedis();
            list = redisClient.zrange(WEB_TASK, 0, -1);
        }
        list.forEach(s -> {
            WebTask task = JSON.parseObject(s, WebTask.class);
            taskList.add(task);
        });
        return this.filter(taskList, brand, userAllHistoryMoney, userid);
    }


    public List<WebTask> filter(List<WebTask> list, String brand, int userAllHistoryMoney, String memberId) {
        List<WebTask> taskList = new ArrayList<>(1);
        List<String> memberTaskList = lbbMemberRecordMapper.getMemberTaskList(memberId);
        List<TaskResult> taskResults = lbbMemberRecordMapper.getMemberTaskListWait(memberId);
        list.forEach(task -> {
            //排除用户做过的和渠道屏蔽，任务量为0的也不显示
            if (task.getSize() > 0
                    && userAllHistoryMoney >= task.getMinMoney()
                    && isPass(task.getStartTime(), task.getEndTime())
                    && !memberTaskList.contains(task.getTaskId())
                    && this.checkPull(task, taskResults)
                    && (task.getChannel().equals(brand) || "".equals(task.getChannel()))) {
                taskList.add(task);
            }
        });
        List<WebTask> result = new ArrayList<>(1);
        if (taskInRedisService.checkRedBookFiter(memberId)) {
            result = taskList.stream().filter(s ->
                    !s.isFilter()).collect(Collectors.toList());
        } else {
            result = taskList;
        }

        result.removeIf(next -> taskInRedisService.checkRedBookByTaksId(next.getTaskId()));
        return result;
    }

    public boolean checkPull(WebTask s, List<TaskResult> memberTaskList) {
        for (TaskResult next : memberTaskList) {
            if (s.getTaskId().equals(next.getTaskId())) {
                return true;
            }
            if (!TextUtil.isEmpty(s.getTaskCategory()) && s.getTaskCategory().equals(next.getTaskCategory())) {
                return false;
            }
        }
        return true;
    }

    /**
     * 检测任务是否在开始时间之内
     *
     * @param startTime
     * @param endTime
     * @return
     */
    private boolean isPass(String startTime, String endTime) {
        if (!TextUtil.isEmpty(startTime) && !TextUtil.isEmpty(endTime)) {
            long now = System.currentTimeMillis();
            String dateHead = DateUtil.dateToString(new Date(), "yyyy-MM-dd");
            long start = stirngToDateLong(dateHead + " " + startTime);
            long end = stirngToDateLong(dateHead + " " + endTime);
            if (now > end || now < start) {
                return false;
            }
        }
        return true;
    }

    private long stirngToDateLong(String cur) {
        if (StringUtils.isBlank(cur)) {
            return 0;
        } else {
            try {
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                return formatter.parse(cur).getTime();
            } catch (ParseException var2) {
                var2.printStackTrace();
                return 0;
            }
        }
    }


    /**
     * 取用户的完成记录列表
     *
     * @param userid 用户ID
     * @param page   　页码
     * @param limit  　限制
     * @return　结果列表
     */
    public List<MemberRecordModel> getTaskHistory(String userid, Long page, Integer limit) {
        List<String> stringList = new ArrayList<>(1);
        stringList.add(TaskCategory.Common.name());
//        stringList.add(TaskCategory.Soso.name());
        stringList.add(TaskCategory.Comments.name());
        stringList.add(TaskCategory.Redbook.name());
        page = (page - 1) * limit;
        LbbMemberRecordExample example = new LbbMemberRecordExample();
        example.createCriteria().andHasDeletedEqualTo(Booleans.False.name()).andMemberIdEqualTo(userid).andCategoryIn(stringList);
        example.setOffset(page);
        example.setLimit(limit);
        example.setOrderByClause("create_time desc");
        List<LbbMemberRecord> memberRecords = lbbMemberRecordMapper.getListByMemberId(example);
        List<MemberRecordModel> logs = new ArrayList<>(1);
        for (LbbMemberRecord vo : memberRecords) {
            String title = "";
            String icon = "";
            BigDecimal money = new BigDecimal(0);
            MemberRecordModel model = new MemberRecordModel();
            model.setCreateTime(vo.getSubmitTime());
            model.setTaskId(vo.getTaskId());
            model.setCategory(vo.getCategory());
            model.setMark(vo.getRemark());
            model.setStatus(vo.getStatus());
            model.setId(vo.getId().intValue());
            if (vo.getSubmitTime() != null) {
                model.setSubmitTime(DateUtil.dateToString(vo.getSubmitTime(), "yyyy-MM-dd HH:mm:ss"));
            } else {
                model.setSubmitTime("");
            }
            switch (vo.getCategory()) {
                case Common:
                    LbbCommon common = commonTaskService.taskInfoByTaskId(vo.getTaskId());
                    if (common == null) {
                        continue;
                    }
                    title = common.getTitle();
                    money = new BigDecimal(AmountUtil.checkAmount(common)).divide(new BigDecimal(100)).setScale(2);
                    icon = common.getIcon();
                    break;
//                case Soso:
//                    LbbSosoWithBLOBs soso = sosoTaskService.taskInfoByTaskId(vo.getTaskId());
//                    if (soso == null) {
//                        continue;
//                    }
//                    title = soso.getTitle();
//                    money = new BigDecimal(AmountUtil.checkAmount(soso)).divide(new BigDecimal(100)).setScale(2);
//                    icon = soso.getIcon();
//                    if (soso.getNeedCheck().equals(Booleans.False)) {
//                        model.setNeedCheck(false);
//                    }
//                    break;
                case Comments:
                    TaskComments co = commentsTaskService.taskInfoByTaskId(vo.getTaskId());
                    if (co == null) {
                        continue;
                    }
                    title = co.getTitle();
                    money = new BigDecimal(co.getWorth()).divide(new BigDecimal(100)).setScale(2);
                    icon = co.getIcon();
                    break;
                default:
                    break;
            }
            model.setMoney(money);
            model.setTitle(title);
            model.setIcon(icon);
            logs.add(model);
        }


        return logs;
    }


    /**
     * 加载所有自营任务到池中
     */
    public void taskInitToRedis() {
        try {

            // SOSO 任务
           // taskInitToRedisSoso();
            // 通用任务
            taskInitToRedisCommon();
            // 评论任务
            taskInitToRedisComments();
            //60分钟更新一次列表
            redisClient.expire(WEB_TASK, 60 * 60);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void taskInitToRedisSoso() {
        String nowDate = DateUtil.dateToString(new Date(), "HH:mm:ss");
        LbbSosoExample sosoExample = new LbbSosoExample();
        sosoExample.createCriteria().andHasDeletedEqualTo(Booleans.False.name()).andStatusEqualTo("On");
        LbbSosoExample.Criteria criteria2 = sosoExample.createCriteria().andHasDeletedEqualTo(Booleans.False.name()).andStatusEqualTo("Wait").andAutoTimeLessThanOrEqualTo(nowDate).andAutoOffTimeGreaterThanOrEqualTo(nowDate);
        sosoExample.or(criteria2);
        List<LbbSoso> sosos = sosoMapper.selectByExample(sosoExample);
        sosos.forEach(vo -> {
            if (vo.getAmount() == 0) {
                vo.setAmount(999999999);
            }
            if (vo.getTotal() == 0) {
                vo.setTotal(999999999);
            }
            if (vo.getSuccessCount() < vo.getTotal() && vo.getTodayAmount() < vo.getAmount()) {
                WebTask webTask = new WebTask();
                if (vo.getNeedCheck().equals(Booleans.False)) {
                    webTask.setNeedCheck(false);
                }
                webTask.setChannel(vo.getChannel());
                webTask.setTitle(vo.getTitle());
                webTask.setSize(taskInRedisService.getTaskAmount(TASK_SOSO_AMOUNT, vo.getTaskId()));
                webTask.setTime(vo.getTaskTime());
                webTask.setLabel(vo.getLabel());
                webTask.setIcon(vo.getIcon());
                webTask.setMinMoney(vo.getMinMoney() == null ? 0 : vo.getMinMoney());
                webTask.setCategory(TaskCategory.Soso);
                webTask.setTaskId(vo.getTaskId());
                webTask.setStartTime(vo.getAutoTime());
                webTask.setEndTime(vo.getAutoOffTime());
                webTask.setType(vo.getType().getIndex());
                redisClient.zadd(WEB_TASK, vo.getSort().doubleValue(), JSON.toJSONString(webTask));
            }
        });

    }

    private void taskInitToRedisCommon() {
        String nowDate = DateUtil.dateToString(new Date(), "HH:mm:ss");
        LbbCommonExample example = new LbbCommonExample();
        example.createCriteria().andHasDeletedEqualTo(Booleans.False.name()).andStatusEqualTo("On");

        LbbCommonExample.Criteria criteria2 = example.createCriteria().andHasDeletedEqualTo(Booleans.False.name()).andStatusEqualTo("Wait").andAutoTimeLessThanOrEqualTo(nowDate).andAutoOffTimeGreaterThanOrEqualTo(nowDate);
        example.or(criteria2);

        List<LbbCommon> commons = commonMapper.selectByExample(example);
        commons.forEach(vo -> {
            if (vo.getAmount() == 0) {
                vo.setAmount(999999999);
            }
            if (vo.getTotal() == 0) {
                vo.setTotal(999999999);
            }
            if (vo.getSuccessCount() < vo.getTotal() && vo.getTodayAmount() < vo.getAmount()) {
                WebTask webTask = new WebTask();
                webTask.setTitle(vo.getTitle());
                if (vo.getIsMultiword().equals(1)) {

                    webTask.setSize(multiwordComponent.getAmountById(vo.getId()));
                } else {
                    webTask.setSize(taskInRedisService.getTaskAmount(TASK_COMMON_AMOUNT, vo.getTaskId()));
                }
                webTask.setChannel(vo.getChannel());
                webTask.setTime(vo.getTaskTime());
                webTask.setLabel(vo.getLabel());
                webTask.setIcon(vo.getIcon());
                webTask.setCategory(TaskCategory.Common);
                webTask.setTaskId(vo.getTaskId());
                webTask.setMinMoney(vo.getMinMoney() == null ? 0 : vo.getMinMoney());
                webTask.setStartTime(vo.getAutoTime());
                webTask.setEndTime(vo.getAutoOffTime());
                webTask.setType(vo.getType().getIndex());
                webTask.setTaskCategory(vo.getTaskCategory());
                if (vo.getLessTime() != null) {
                    if (vo.getLessTime() != 0) {
                        webTask.setFilter(true);
                    }
                }
                redisClient.zadd(WEB_TASK, vo.getSort().doubleValue(), JSON.toJSONString(webTask));
            }
        });
    }

    private void taskInitToRedisComments() {
        String nowDate = DateUtil.dateToString(new Date(), "HH:mm:ss");
        TaskCommentsExample taskCommentsExample = new TaskCommentsExample();
        taskCommentsExample.createCriteria().andHasDeletedEqualTo(0).andStatusEqualTo(1);
        TaskCommentsExample.Criteria criteria2 = taskCommentsExample.createCriteria().andHasDeletedEqualTo(0).andStatusEqualTo(2).andAutoTimeLessThanOrEqualTo(nowDate).andAutoOffTimeGreaterThanOrEqualTo(nowDate);
        taskCommentsExample.or(criteria2);
        List<TaskComments> comments = taskCommentsMapper.selectByExample(taskCommentsExample);
        comments.forEach(vo -> {
            if (vo.getAmount() == 0) {
                vo.setAmount(999999999);
            }
            if (vo.getTotal() == 0) {
                vo.setTotal(999999999);
            }
            if (vo.getSuccessCount() < vo.getTotal() && vo.getTodayAmount() < vo.getAmount()) {
                WebTask webTask = new WebTask();
                webTask.setChannel(vo.getBrand());
                webTask.setTitle(vo.getTitle());
                webTask.setSize(taskInRedisService.getTaskAmount(TASK_COMMENTS_AMOUNT, vo.getTaskId()));
                webTask.setTime(vo.getTaskTime());
                webTask.setLabel(vo.getLabel());
                webTask.setIcon(vo.getIcon());
                webTask.setMinMoney(vo.getMinMoney() == null ? 0 : vo.getMinMoney());
                webTask.setStartTime(vo.getAutoTime());
                webTask.setEndTime(vo.getAutoOffTime());
                webTask.setCategory(TaskCategory.Comments);
                webTask.setTaskId(vo.getTaskId());
                webTask.setType(vo.getType().getIndex());
                if (vo.getLessTime() != null) {
                    if (vo.getLessTime() != 0) {
                        webTask.setFilter(true);
                    }
                }
                redisClient.zadd(WEB_TASK, vo.getSort().doubleValue(), JSON.toJSONString(webTask));
            }
        });

    }


    /**
     * 获取用户截图任务历史记录
     *
     * @param memberId
     * @param category
     * @param page
     * @param limit
     * @return
     */
    public List<MemberRecordModel> getTaskHistoryV2(String memberId, Integer category, Long page, Integer limit) {
        List<String> stateList = new ArrayList<>();
        if (category == HistoryTaskCategory.WAIT.getIndex()) {
            stateList.add(TaskStatus.WaitSumbit.name());
            stateList.add(TaskStatus.WaitCheckout.name());
            //待提交添加可重做记录
            stateList.add(TaskStatus.CheckFailure.name());
        } else if (category == HistoryTaskCategory.SUCCESS.getIndex()) {
            stateList.add(TaskStatus.CheckSuccess.name());
        } else if (category == HistoryTaskCategory.FAIL.getIndex()) {
            stateList.add(TaskStatus.CheckFailure.name());
            stateList.add(TaskStatus.WaitSumbit.name());
        }
        page = (page - 1) * limit;
        List<MemberTaskModel> memberRecords = lbbMemberRecordMapper.getRecordsByMemberId(page, limit, memberId, stateList);
        List<MemberRecordModel> memberRecordModels = new ArrayList<>(1);
        for (MemberTaskModel vo : memberRecords) {
            MemberRecordModel model = new MemberRecordModel();
            model.setCreateTime(vo.getSubmitTime());
            model.setTaskId(vo.getTaskId());
            model.setCategory(vo.getCategory());
            model.setMark(vo.getRemark());
            model.setStatus(vo.getStatus());
            model.setMultiple(vo.getMultiple());

            if (category == HistoryTaskCategory.WAIT.getIndex() && vo.getStatus().equals(TaskStatus.WaitSumbit)) {
                if (vo.getRedo() >= 1) {
                    if (isOverTime(vo.getCreateTime(), Optional.ofNullable(vo.getReleaseTime()).orElse(30))) {
                        continue;
                    }
                }
            }
            if (category == HistoryTaskCategory.WAIT.getIndex() && vo.getStatus().equals(TaskStatus.CheckFailure)) {
                //筛选添加失败可重做的任务
                if (vo.getIsRedo() == 1) {
                    //任务可重做
                    if (vo.getRedo() < vo.getFailTimes() && vo.getFailTimes() > 0) {
                        //重做次数小等于最大重做次数
                        model.setStatus(TaskStatus.redo);
                    } else {
                        continue;
                    }
                } else {
                    continue;
                }
            }
            if (category == HistoryTaskCategory.FAIL.getIndex()) {
                //筛选去掉可重做任务
                if (vo.getIsRedo() >= 1 && !vo.getStatus().equals(TaskStatus.WaitSumbit)) {
                    //任务可重做
                    if (vo.getRedo() < vo.getFailTimes() && vo.getFailTimes() > 0) {
                        //重做超时
                        continue;
                    }
                }
                if (vo.getIsRedo() >= 1 && vo.getStatus().equals(TaskStatus.WaitSumbit)) {
                    if (isOverTime(vo.getCreateTime(), Optional.ofNullable(vo.getReleaseTime()).orElse(30))) {
                        model.setStatus(TaskStatus.CheckFailure);
                    } else {
                        continue;
                    }
                }
            }
            model.setId(vo.getId().intValue());
            if (vo.getSubmitTime() != null) {
                model.setSubmitTime(DateUtil.dateToString(vo.getSubmitTime(), "yyyy-MM-dd HH:mm:ss"));
            } else {
                model.setSubmitTime("");
            }

            model.setMoney(new BigDecimal(vo.getUserMoney()).multiply(new BigDecimal(vo.getMultiple())).divide(new BigDecimal(100)).setScale(2));
            model.setTitle(vo.getTaskTitle());
            model.setIcon(vo.getIcon());
            model.setQq(vo.getQq());
            model.setCheckTime(vo.getCheckTime());
            memberRecordModels.add(model);
        }


        return memberRecordModels;
    }

    //是否超时
    public Boolean isOverTime(Date time, Integer releaseTime) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime overtime = LocalDateTime.parse(ShowDateUtil.afterMinute(time, releaseTime), df);
        if (LocalDateTime.now().isAfter(overtime)) {
            return true;
        }
        return false;
    }


    public static final String NEW_WEB_TASK = "NEW_WEB_TASK";

    /**
     * 新取任务列表
     *
     * @param memberId
     * @param brand
     * @param userAllHistoryMoney
     * @return
     */
    public List<WebTask> getNewTaskList(String memberId, String brand, int userAllHistoryMoney, String status, Integer calssifyId, Integer tagId) {
        LbbMember memberByMemberId = lbbMemberMapper.getMemberByMemberId(memberId);
        List<WebTask> list;
        //String webTask = redisClient.hget(NEW_WEB_TASK, calssifyId + ":" + tagId);
        String webTask = redisSdkClient.hget(NEW_WEB_TASK, calssifyId + ":" + tagId);
        if (!TextUtil.isEmpty(webTask)) {
            list = JSON.parseArray(webTask, WebTask.class);
        } else {
            list= newTaskInitToRedis(status, calssifyId, tagId);
        }
        if (list.size() == 0) {
            return list;
        }
        return this.newFilter(list, brand, userAllHistoryMoney, memberId,memberByMemberId.getPhone());
    }

    /**
     * 获取上架任务
     */
    private List<WebTask> taskInitToRedisClassify(String status, Integer calssifyId, Integer tagId) {
        List<WebTask> list = new ArrayList<>();
        LocalDateTime starTime = LocalDateTime.now().minusDays(3);
        LocalDateTime endTime = LocalDateTime.now();
        List<TaskScreenshot> taskScreenshots = taskScreenshotMapper.selectByCondition(status, calssifyId, tagId, starTime, endTime);
        for (TaskScreenshot vo : taskScreenshots) {
            if (vo.getAmount() == 0) {
                vo.setAmount(999999999);
            }
            if (vo.getTotal() == 0) {
                vo.setTotal(999999999);
            }
            if (vo.getSuccessCount() < vo.getTotal() && vo.getTodayAmount() < vo.getAmount()) {
                WebTask webTask = new WebTask();
                if (TaskCategory.Common.name().equals(vo.getCategory())) {
                    Integer taskCommonAmount = taskInRedisService.getTaskAmount(TASK_COMMON_AMOUNT, vo.getTaskId());
                    webTask.setSize(taskCommonAmount);
                }
                if (TaskCategory.Comments.name().equals(vo.getCategory())) {
                    Integer taskCommonsAmount = taskInRedisService.getTaskAmount(TASK_COMMENTS_AMOUNT, vo.getTaskId());
                    webTask.setSize(taskCommonsAmount);
                }
                if (TaskCategory.Soso.name().equals(vo.getCategory())) {
                    Integer taskSosoAmount = taskInRedisService.getTaskAmount(TASK_SOSO_AMOUNT, vo.getTaskId());
                    webTask.setSize(taskSosoAmount);
                }
                webTask.setChannel(vo.getChannel());
                webTask.setTitle(vo.getTitle());
                webTask.setTime(vo.getTaskTime());
                webTask.setLabel(vo.getLabel());
                webTask.setIcon(vo.getIcon());
                webTask.setMinMoney(vo.getMinMoney() == null ? 0 : vo.getMinMoney());
                webTask.setCategory(TaskCategory.getByName(vo.getCategory()));
                webTask.setTaskId(vo.getTaskId());
                webTask.setStartTime(vo.getAutoTime());
                webTask.setEndTime(vo.getAutoOffTime());
                webTask.setType("Cash".equals(vo.getType()) ? 0 : 1);
                webTask.setTaskclassifyId(vo.getTaskCategoryId());
                webTask.setReleaseTime(vo.getModifyTime());
                webTask.setTaskTagIds(vo.getTaskTagRelationList());
                webTask.setTaskIntroduce(vo.getTaskIntroduce());
                list.add(webTask);
            }
        }

        redisClient.hset(NEW_WEB_TASK, calssifyId + ":" + tagId, JSON.toJSONString(list));
        redisClient.expire(NEW_WEB_TASK, 3600);
        return list;
    }

    /**
     * 任务列表过滤
     *
     * @param list
     * @param brand
     * @param userAllHistoryMoney
     * @param memberId
     * @return
     */
    public List<WebTask> newFilter(List<WebTask> list, String brand, int userAllHistoryMoney, String memberId,String phone) {
        //过滤Soso
        list = list.stream().filter(vo -> !vo.getCategory().equals(TaskCategory.Soso)).collect(Collectors.toList());
        List<WebTask> taskList = new ArrayList<>(1);
        List<String> memberTaskList = lbbMemberRecordMapper.getTaskRecordList(memberId);
        List<String> memberToRedoList = lbbMemberRecordMapper.getToRedo(memberId);
        List<String> listRecord = lbbMemberRecordMapper.getToRecord(memberId);
        List<TaskResult> taskResults = lbbMemberRecordMapper.getMemberTaskListWait(memberId);
        List<String> taskIds = lbbMemberRecordMapper.getByPhone(phone);

        //查询
        list.forEach(task -> {
            //排除用户做过的和渠道屏蔽，任务量为0的也不显示,同一个手机号在一个应用上做过的任务其他应用列表不显示
            if (task.getSize() > 0
                    && userAllHistoryMoney >= task.getMinMoney()
                    && isPass(task.getStartTime(), task.getEndTime())
                    && !memberTaskList.contains(task.getTaskId())
                    && !memberToRedoList.contains(task.getTaskId())
                    && !listRecord.contains(task.getTaskId())
                    && this.checkPull(task, taskResults)
                    && !taskIds.contains(task.getTaskId())
                    && (task.getChannel().equals(brand) || "".equals(task.getChannel()))) {
                taskList.add(task);
            }
        });
        List<WebTask> result = new ArrayList<>(1);
        if (taskInRedisService.checkRedBookFiter(memberId)) {
            result = taskList.stream().filter(s ->
                    !s.isFilter()).collect(Collectors.toList());
        } else {
            result = taskList;
        }

        result.removeIf(next -> taskInRedisService.checkRedBookByTaksId(next.getTaskId()));
        return result;
    }


    /**
     * 新加载所有自营任务到池中
     */
    public List<WebTask> newTaskInitToRedis(String status, Integer calssifyId, Integer tagId) {
            return taskInitToRedisClassify(status, calssifyId, tagId);
    }


    public ShareModel getShareModel(String taskId) {
        TaskScreenshot taskScreenshot = taskScreenshotMapper.selectByTaskId(taskId);
        ShareModel shareModel = new ShareModel();
        shareModel.setIcon(taskScreenshot.getIcon());

        if (TaskCategory.Common.name().equals(taskScreenshot.getCategory())) {
            Integer taskCommonAmount = taskInRedisService.getTaskAmount(TASK_COMMON_AMOUNT, taskScreenshot.getTaskId());
            shareModel.setNum(taskCommonAmount+"份");
        }else if (TaskCategory.Comments.name().equals(taskScreenshot.getCategory())) {
            Integer taskCommonsAmount = taskInRedisService.getTaskAmount(TASK_COMMENTS_AMOUNT, taskScreenshot.getTaskId());
            shareModel.setNum(taskCommonsAmount+"份");
        }else{
            shareModel.setNum("0份");
        }


        shareModel.setTaskTime(taskScreenshot.getTaskTime()+"分钟");
        shareModel.setShowMoney(taskScreenshot.getLabel()+"");
        shareModel.setShareIntroduce(taskScreenshot.getShareIntroduce());
        shareModel.setUrl("");
        shareModel.setTitle(taskScreenshot.getTitle());
        shareModel.setType(taskScreenshot.getCategory());
        return shareModel;
    }
}
